import getApiData, { getApisData } from "@/services/cpasRequest";

/**
 * 获取组成信息列表
 * @param params 
 * @returns 
 */
export async function getDateList(params: {
  zcbfid: string;
}) {
  const res = await getApiData('home.zcbfAuditSection', params);
  if (res.ok) {
    return res.data;
  };
  return [];
}

// 获取账套
export async function getAccountInfo(params: {
  dbname: string;
}) {
  const res = await getApiData("account.accountInfo.getDataList", params);
  if (res.ok) {
    return res.data.sort((a: any, b: any) => a.id - b.id);
  }
  return {};
}

export async function getSubjectType(params: {
  dbname: string;
  Data_ID: string;
}) {
  const res = await getApiData("account.cutOffDate.get", params);
  if (res.ok) {
    return res.data;
  }
  return {};
}


// 添加数据
export async function getDateAdd(params: {
  nd: string;
  ADateE: string;
  ADateS: string;
  dbname: string
}) {
  // 添加模板
  const data = [];
  data.push({
    rowState: "insert",
    nd: params.nd,
    ADateE: params.ADateE,
    ADateS: params.ADateS,
    ID: params.dbname
  })
  const r = await getApiData("sqlApi.update", {
    name: "l_dwndInfo_detail",
    dbName: params.dbname,
    rows: data
  });
  if (r.ok) return r.ok;
}

// 更新数据
//   export async function getDateUpdate(params: {
//     nd: string;
//     ADateE: string;
//     ADateS: string;
//     dbname: string;
//     id: string
//   }) {
//       // 插入的模板
//       const data = [];
//         data.push({
//           rowState: "update",
//           nd: params.nd,
//           ADateE:  params.ADateE,
//           ADateS: params.ADateS,
//           where: {
//             autoID:params.id,
//           }
//         });

//         const postData = {
//           apis: [
//               {
//                   name: 'sqlApi.update',
//                   params: { name: `[${params.dbname}]..l_dwndInfo_detail`, rows: data }
//               },
//           ],
//           reload: true,
//           params: {}
//       }
//       const r = await getApisData(postData);
//       // const r = await getApisData("sqlApi.update", {
//       //   name: "l_dwndInfo_detail",
//       //   dbName: params.dbname,
//       //   rows: data
//       // });
//       if (r.ok)  return r.ok;
// }


export async function DateUpdate(
  dbname: string,
  paramsData: any[],
) {
  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: `[${dbname}]..period_settings`, rows: paramsData }
      },
    ],
    reload: true,
    params: {}
  }
  const resApis = await getApisData(postData);
  const resUpdate = resApis.data['sqlApi.update']

  if (resApis.ok && resUpdate.ok) {
    return Promise.resolve({
      data: 'period_settings, 更改信息成功！',
      success: true,
    });
  }
  return Promise.resolve({
    data: resApis.data,
    success: false,
  });
}

export async function PeriodUpdate(
  dbname: string,
  paramsData: any[],
) {
  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: `[${dbname}]..l_dwndInfo_detail`, rows: paramsData }
      },
    ],
    reload: true,
    params: {}
  }
  const resApis = await getApisData(postData);
  const resUpdate = resApis.data['sqlApi.update']

  if (resApis.ok && resUpdate.ok) {
    return Promise.resolve({
      data: 'l_dwndInfo_detail, 更改信息成功！',
      success: true,
    });
  }
  return Promise.resolve({
    data: resApis.data,
    success: false,
  });
}

// 删除数据
export async function getDateDelete(params: {
  dbname: string;
  id: string
}) {
  // 删除的模板
  const data = [];
  data.push({
    rowState: "delete",
    data_id: params.id,
  })
  const r = await getApiData("sqlApi.update", {
    name: "l_dwndInfo_detail",
    dbName: params.dbname,
    rows: data
  });
  if (r.ok) return r.ok;
}


